﻿Imports System.Xml.Serialization
Imports System.Text


Namespace Configuration

    ''' <summary>
    ''' 用于储存编码的单一列表
    ''' </summary>
    ''' <remarks>
    ''' 支持序列化到XML
    ''' 集合的内容不会存在重复数据
    ''' </remarks>
    <Serializable()>
    Public Class EncodingList
        Inherits List(Of Encoding)
        Implements IXmlSerializable, ICloneable


        Public Sub New()

        End Sub
        Public Sub New(ByVal capacity As Integer)
            MyBase.New(capacity)

        End Sub
        Public Sub New(ByVal collection As IEnumerable(Of Encoding))
            MyBase.New(collection.Distinct)

        End Sub

        ''' <summary>
        ''' 将对象添加到 EncodingList 的结尾处。
        ''' </summary>
        ''' <param name="item">
        ''' 要添加到 EncodingList 的末尾处的Encoding对象。
        ''' 如果该值为 Nothing，则不会添加到集合。
        ''' </param>
        ''' <remarks></remarks>
        Public Shadows Sub Add(item As Encoding)
            If item IsNot Nothing AndAlso Not Me.Contains(item) Then
                MyBase.Add(item)
            End If
        End Sub

        ''' <summary>
        ''' 将指定集合的元素添加到 EncodingList 的末尾。
        ''' </summary>
        ''' <param name="collection">
        ''' 一个集合，其元素应被添加到 EncodingList 的末尾。
        ''' 集合自身不能为 null，如果元素中包含 Nothing，则 Nothing 不会添加到集合。
        ''' </param>
        ''' <remarks></remarks>
        Public Shadows Sub AddRange(collection As IEnumerable(Of Encoding))
            If collection IsNot Nothing Then
                Dim list = collection.Where(Function(x) x IsNot Nothing AndAlso Not Me.Contains(x)).Distinct
                MyBase.AddRange(list)
            End If
        End Sub


        Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
            Return Nothing
        End Function

        Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
            Dim xml = XElement.Load(reader)

            Dim encodings = xml...<CodePage>.Select(Function(x) (x.Value))

            Dim code As Integer
            For Each e In encodings
                If Not String.IsNullOrEmpty(e) AndAlso Integer.TryParse(e, code) Then
                    Me.Add(Encoding.GetEncoding(code))
                End If
            Next


        End Sub

        Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
            Dim xml = <Encodings>
                          <%= From encoding In Me
                              Select <CodePage><%= encoding.CodePage %></CodePage>
                          %>
                      </Encodings>

       
            xml.WriteTo(writer)

        End Sub

#Region "Clone 实现"

        Function Clone() As EncodingList
            Return New EncodingList(Me)
        End Function

        Private Function ImeClone() As Object Implements System.ICloneable.Clone
            Return Me.Clone
        End Function

#End Region

    End Class


End Namespace

